﻿using System;

namespace YArchitech.Revit.MEP.Electric.Calculator
{
	public class IllCalculate
	{
		public IllCalculate(RoomData data1, LampData data2, LightSourceData data3, IllCalType type, int lampNumber = 0)
		{
			this.roomData = data1;
			this.lampData = data2;
			this.lightSourceData = data3;
			this.resultData = new CalResultData();
			this.resultData.LampNumber = lampNumber;
			this.calType = type;
		}

		public void Cal(int lampNum)
		{
			this.resultData.LampNumber = lampNum;
			this.resultData.Lux = (double)(this.resultData.LampNumber * this.lightSourceData.Number) * this.lightSourceData.LuminousFlux * this.lampData.UseFactor * this.lampData.MaintenanceFactor / this.roomData.RoomArea;
			this.resultData.Lpd = (double)this.resultData.LampNumber * ((double)this.lightSourceData.Number * this.lightSourceData.Power + this.lampData.BallastPower) / this.roomData.RoomArea;
			this.resultData.LpdEx = this.roomData.Lux * this.resultData.Lpd / this.resultData.Lux;
			if (this.resultData.Lux < this.roomData.Lux * 0.9)
			{
				this.resultData.LuxChecked = CheckedState.eNoMeet;
			}
			else if (this.resultData.Lux > this.roomData.Lux * 1.1)
			{
				this.resultData.LuxChecked = CheckedState.eNoMeet;
			}
			else
			{
				this.resultData.LuxChecked = CheckedState.eMeet;
			}
			if (this.roomData.Lpd < 0.0001)
			{
				this.resultData.LpdChecked = CheckedState.eMeet;
				return;
			}
			if (this.resultData.LpdEx > 0.0001 && this.resultData.LpdEx < this.roomData.Lpd)
			{
				this.resultData.LpdChecked = CheckedState.eMeet;
				return;
			}
			this.resultData.LpdChecked = CheckedState.eNoMeet;
		}

		public void Cal()
		{
			if (this.calType == IllCalType.eIllCal)
			{
				double a = this.roomData.Lux * this.roomData.RoomArea / ((double)this.lightSourceData.Number * this.lightSourceData.LuminousFlux * this.lampData.UseFactor * this.lampData.MaintenanceFactor);
				this.resultData.LampNumber = (int)Math.Ceiling(a);
			}
			this.resultData.Lux = (double)(this.resultData.LampNumber * this.lightSourceData.Number) * this.lightSourceData.LuminousFlux * this.lampData.UseFactor * this.lampData.MaintenanceFactor / this.roomData.RoomArea;
			this.resultData.Lpd = (double)this.resultData.LampNumber * ((double)this.lightSourceData.Number * this.lightSourceData.Power + this.lampData.BallastPower) / this.roomData.RoomArea;
			this.resultData.LpdEx = this.roomData.Lux * this.resultData.Lpd / this.resultData.Lux;
			if (this.resultData.Lux < this.roomData.Lux * 0.9)
			{
				this.resultData.LuxChecked = CheckedState.eNoMeet;
			}
			else if (this.resultData.Lux > this.roomData.Lux * 1.1)
			{
				this.resultData.LuxChecked = CheckedState.eNoMeet;
			}
			else
			{
				this.resultData.LuxChecked = CheckedState.eMeet;
			}
			if (this.roomData.Lpd < 0.0001)
			{
				this.resultData.LpdChecked = CheckedState.eMeet;
				return;
			}
			if (this.resultData.LpdEx > 0.0001 && this.resultData.LpdEx < this.roomData.Lpd)
			{
				this.resultData.LpdChecked = CheckedState.eMeet;
				return;
			}
			this.resultData.LpdChecked = CheckedState.eNoMeet;
		}

		public CalResultData ResultData
		{
			get
			{
				return this.resultData;
			}
		}

		private RoomData roomData;

		private LampData lampData;

		private LightSourceData lightSourceData;

		private CalResultData resultData;

		private IllCalType calType;
	}
}
